package edu.cs335pl.questionnaire.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JWTutils {

    //secrect key!
    private static final String key = "dhsadsaa!#@!221#@!$%rweqrqw";
    private static final long EXPIRE_TIME = 8 * 60 * 60 * 1000;

//    public static String getToken(HashMap<String,String> map){
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DATE,2); //Seven days for expire
//        JWTCreator.Builder builder = JWT.create();
//        map.forEach((k,v)->{
//            builder.withClaim(k,v);
//            });
//
//        String token = builder.withExpiresAt(calendar.getTime()).sign(Algorithm.HMAC256(key));
//        return token;
////     String token = JWT.create()
////                .withHeader(map)
////                .withClaim("id",21)
////                .withClaim("company","IBM")
////                .withExpiresAt(calendar.getTime())
////                .sign(Algorithm.HMAC256("key"));
////                 return token;
//       }
        public static String sign(long id,String account) {
            //            过期时间
            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            //            私钥及加密算法
            Algorithm algorithm = Algorithm.HMAC256(key);
            //            设置头部信息
            Map<String, Object> header = new HashMap<>(2);
            header.put("typ", "JWT");
            header.put("alg", "HS256");
            // 附带username，userId信息，生成签名
            return JWT.create()
                    .withHeader(header)
                    .withClaim("id", id)
                    .withClaim("account",account)
                    .withExpiresAt(date)
                    .sign(algorithm);
        }



    public static boolean verify(String token) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(key);
            JWTVerifier verifier = JWT.require(algorithm)
                    .build();
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (JWTDecodeException exception) {
            return false;
        }
    }
    public static long getId(String token){
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("id").asLong();
        }
        catch (JWTDecodeException e){
            return 0;
        }
    }

    public static String getAccount(String token){
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("account").asString();
        }
        catch (JWTDecodeException e){
            return null;
        }
    }



//       //Check if token is legal
//    public static void validate(String token){
//           JWT.require(Algorithm.HMAC256(key)).build().verify(token);
//       }
//
//
//       //Fetch information from token
//    public  static DecodedJWT getTokenInfo(String token){
//        DecodedJWT verify = JWT.require(Algorithm.HMAC256(key)).build().verify(token);
//        return verify;
//     }
//
//}
}
